import axios from 'axios'
import Utils from '@/common/utils.js';
import Api from '@/api/index.js'
export default {
  data() {
    return {
      rowData: [],
      filterSchool: [],
      planSchoolName: Utils.session('planSchoolName'),
      majorTableData: {
        majors: []
      },
      loading: false,
      searchByCode: false,
      isCode: false
    }
  },
  methods: {
    // 处理学校数据到最终使用数据
    async originalToTbData() {
      let that = this
      this.majorTableData.majors = [];
      this.$vux.loading.show()
      // axios.get('https://86edu.oss-cn-hangzhou.aliyuncs.com/json/test/schoolPlan20181130.json?v=20181130')

      Api.getVersion().then(v => {
        console.log('============',v)
        axios.get(`//86edu.oss-cn-hangzhou.aliyuncs.com/json/20181101/2019schoolPlan.json?v=${v.message}`)
          .then((res) => {
            this.$vux.loading.hide();
            if (res.status === 200) {
              that.rowData = res.data
            }
          })
          .catch((err) => {
            console.log(err)
          })
      })

    },
    filterSubjectAll(schoolName) {
      return this.rowData.filter((item) => {
        return item.school === schoolName
      })
    },
    // 筛选专业数据
    filterSubjectByName(schoolName) {
      let _filter = []
      this.rowData.map((item) => {
        if (this.interceptName(item.school).indexOf(schoolName) >= 0) {
          _filter.push({
            value: item.school,
            code: item.code
          })
        }
      })
      return _filter
    },
    // 根据院校代码&国家代码筛选专业数据
    filterSubjectByCode(schoolCode) {
      let _filter = []
      this.rowData.map((item) => {
        if (item.incode === schoolCode || item.code === schoolCode) {
          _filter.push({
            value: item.school,
            code: item.code
          })
        }
      })
      return _filter
    },
    // 筛选得出专业
    filterSchoolMethod(queryStr, cb) {
      if (queryStr !== '') {
        this.loading = true
        if (parseInt(queryStr) === queryStr / 1 && queryStr.length >= 4) {
          this.filterSchool = this.filterSubjectByCode(queryStr)
          this.isCode = true
        } else {
          this.filterSchool = this.filterSubjectByName(this.interceptName(queryStr))
          this.isCode = false
        }
        this.loading = false
      } else {
        this.filterSchool = []
        this.loading = false
      }
      if (this.isCode && this.filterSchool.length === 1) {
        this.searchByCode = true
        this.planSchoolName = this.filterSchool[0].value
      }
    },
    // 下拉框出现/隐藏时触发
    selectChange() {
      if (this.planSchoolName) {
        console.log(this.filterSubjectAll(this.planSchoolName)[0])
        this.majorTableData = this.filterSubjectAll(this.planSchoolName)[0]
        if (this.majorTableData) {
          Utils.session('majorTableData', JSON.stringify(this.majorTableData))
          Utils.session('planSchoolName', this.planSchoolName)
          this.majorTableData = null
          // this.$store.commit('SET_PLANSEARCHSCHOOL', this.planSchoolName)
          this.$router.push({ name: 'planResult' })
        } else {
          // this.$message.error('未找到相关的院校专业')
          this.$vux.toast.show({
            text: '未找到相关的院校',
            type: 'text',
            width: '80%'
          })
        }
      }
    },
    // 选中值发生变化时触发
    handleSelect() {
      this.selectChange()
    },
    // 截取学校名称 ()后面的都不要
    interceptName(uName) {
      let idx = uName.indexOf('(')
      return idx > 0 ? uName.substr(0, idx) : uName
    }
  }
}
